package cn.fz.mpdemo.controller;

import cn.fz.mpdemo.domain.po.EmpEntity;
import cn.fz.mpdemo.domain.query.LoginQuery;
import cn.fz.mpdemo.domain.vo.ResultVO;
import cn.fz.mpdemo.service.EmpService;
import cn.fz.mpdemo.utils.JwtUtils;
import cn.hutool.core.util.ObjUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Tag(name = "用户登陆接口模块")
public class LoginController {
    private final EmpService empService;

    // https://doc.xiaominfo.com/docs/faq/v4/knife4j-parameterobject-flat-param
    @Operation(description = "用户登陆接口",summary = "用xx户登陆接口")
    @PostMapping("/login")
    public ResultVO login(@RequestBody LoginQuery loginQuery){
        EmpEntity emp = empService.login(loginQuery.getUsername(),loginQuery.getPassword());
        if (ObjUtil.isEmpty(emp)){
            return ResultVO.failed("用户名或密码错误");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("username",emp.getUsername());
        map.put("name",emp.getName());
        map.put("id",emp.getId());
        String token = JwtUtils.createToken(map);
        return ResultVO.success(token);
    }
}
